﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLibArtreve.dal;
using System.Configuration;
using CLibArtreve.models;
using System.Data;

namespace CLibArtreve.bll
{
    public class ArtCategoryBLL
    {
        #region VARIABLES
        private string _connectionString;        
        private ArtCategoryDAL _adapter;
        #endregion

        #region PROPERTIES
        public string ConnectionString
        {
            get {
                if (_connectionString == null)
                    _connectionString = ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
                return _connectionString; 
            }
            set { _connectionString = value; }
        }
        public ArtCategoryDAL Adapter
        {
            get
            {
                if (_adapter == null)
                    _adapter = new ArtCategoryDAL(ConnectionString);
                return _adapter;
            }
        }
        #endregion

        #region CONSTRUCTOR
        public ArtCategoryBLL(string connectionString)
        {
            ConnectionString = connectionString;
        }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "BUSINESS LOGIC LAYER: " + this.GetType().ToString();
        }
        #endregion

        #region GET METHODS
        public IList<ArtCategory> GetAllArtCategories()
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetAllArtCategories();
                //MAKE A LIST
                IList<ArtCategory> list = new List<ArtCategory>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public ArtCategory GetArtByCategoryId(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetArtByCategoryId(id);

                //IF DATASET EXISTS EN IT HAS ROWS RETURN THE ROW
                if(ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return GenerateArtFromDataRow(row);
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public ArtCategory GetCategoryByArtId(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetCategoryByArtId(id);

                //IF DATASET EXISTS EN IT HAS ROWS RETURN THE ROW
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return GenerateArtFromDataRow(row);
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }  
        #endregion

        #region INSERT METHODS
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int InsertArtCategory(ArtCategory artCategory)
        {
            try
            {
                return Adapter.InsertArtCategory(artCategory.ArtId, artCategory.Id);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region UPDATE METHODS
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int UpdateArtCategory(ArtCategory artCategory)
        {
            try
            {
                return Adapter.UpdateArtCategory(artCategory.ArtId, artCategory.Id);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region UTILITIES
        private ArtCategory GenerateArtFromDataRow(DataRow row)
        {
            ArtCategory obj = new ArtCategory();

            // NAAR NOT NULL VERANDEREN?
            obj.Id = Convert.ToInt64(row["arts_art_id"]);
            obj.ArtId = Convert.ToInt64(row["categories_category_id"]);
            
            return obj;
        }
        #endregion
    }
}
